<?php
/**
 * oscshop2 B2C电子商务系统
 *
 *
 * @author    
 *
 */
namespace osc\admin\controller;
use osc\common\controller\AdminBase;
use think\Db;
class ArticleCategory extends AdminBase{
	
	protected function _initialize(){
		parent::_initialize();
		$this->assign('breadcrumb1','文章');
		$this->assign('breadcrumb2','文章类别');
	}
	
    public function index(){
    	
		$pid=input('param.pid');
		
		if(!$pid){
			$pid=0;
		}
		
		$list = Db::name('ArticleCategory')->where('pid',$pid)->paginate(config('page_num'));
		$this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
		$this->assign('list', $list);
		
		return $this->fetch();   
    }
	
	public function add(){
		
		if(request()->isPost()){
			//var_dump(input('post.'));
			$data = input('post.');
			//die;
			
			$dt['image']=$data['image'];
			$dt['pid']=$data['pid'];
			$dt['name']=$data['name'];
			$dt['meta_keyword']=$data['meta_keyword'];
			$dt['meta_description']=$data['meta_description'];
			//$model=osc_model('admin','ArticleCategory');
			$ret=Db::name('article_category')->insert($dt);
			
			//$resault=$model->add(input('post.'));
			
			
				if($ret){								
					storage_user_action(UID,admin('username'),config('BACKEND_USER'),'新增了文章分类');						
					return ['success'=>'新增成功','action'=>'add'];				
				}else{			
					return ['error'=>'新增失败'];
				}
				
			
			
		}
		$this->assign('category',osc_system()->get_article_category_tree());
		$this->assign('action',url('ArticleCategory/add'));
		$this->assign('crumbs','新增');
		return $this->fetch('edit');
	}
	
	public function edit(){
		
		if(request()->isPost()){
			
			$model=osc_model('admin','ArticleCategory');
			
			$resault=$model->edit(input('post.'));
			
			if(isset($resault['error'])){
				return ['error'=>$resault['error']];
			}else{
				
				if($resault){								
					storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了文章分类');						
					return ['success'=>'修改成功','action'=>'edit'];				
				}else{			
					return ['error'=>'修改失败'];
				}
				
			}
			
		}
		
		$this->assign('category',osc_system()->get_article_category_tree());
		
		$this->assign('cat',Db::name('ArticleCategory')->find((int)input('param.id')));
		
		$this->assign('action',url('ArticleCategory/edit'));
		$this->assign('crumbs','修改');
		return $this->fetch('edit');
	}
	
	//删除分类
	function del(){	
			
		$r=osc_model('admin','ArticleCategory')->del_category((int)input('param.id'));	
		
		if($r){
			
			storage_user_action(UID,admin('username'),config('BACKEND_USER'),'删除了文章分类'.input('get.id'));
			
			$this->redirect('ArticleCategory/index');
			
		}else{
			
			return $this->error('删除失败！',url('ArticleCategory/index'));
		}		
		
	}

	//更新排序
	function update_sort(){
		$data=input('post.');
		
		$update['id']=(int)$data['cid'];
		$update['sort_order']=(int)$data['sort'];
		
		if(Db::name('ArticleCategory')->update($update)){
			
			storage_user_action(UID,admin('username'),config('BACKEND_USER'),'更新了文章分类排序');
			
			return true;
		}		
	}
}
